#! /bin/bash
#
# description: delete expired file, based on cam_id, expire time, which are recorded in file:cam_info.txt
# note: this script run only at backup node or backup node group.
# usage:.clean_expired_file.sh /var/csv-data/metadata/cam_info
#
PERIOD_MAX=1000
PERIOD_MIN=1

backup_node_recorder_mark="csv-recorder\ --dau_id=0"

function clean_garbage()
{
    expired_time=$1
    if [ -z "$expired_time" ]; then
        echo $expired_time
        return 0
    fi   
    
    cam_id=$2
    if [ -z "$cam_id" ]; then
        echo $cam_id
        return 0
    fi

    backup_node_disk_list=`ps -ef | grep "$backup_node_recorder_mark" | awk '{print $11}' | awk -F "=" '{print $NF}'`
    if [[ -z $backup_node_disk_list ]]; then
        echo "this not backup node, or backup node disk list is null, stop."
        return 0
    else
         echo "backup node disk list: "$backup_node_disk_list   
    fi

    backup_node_disk_num=`ps -ef | grep "$backup_node_recorder_mark" | awk '{print $11}' | awk -F "=" '{print $NF}' | awk -F "," '{print NF}'`
    echo $backup_node_disk_num
        
    OLD_IFS="$IFS"
    IFS=","
    array=($backup_node_disk_list)
    IFS="$OLD_IFS"
    for backup_node_disk in  ${array[@]}
    do
        if [[ -z "$backup_node_disk" ]]; then
            continue
        fi

        echo "backup node disk: $backup_node_disk "
        
        if [[ ! -d  $backup_node_disk/$cam_id ]]; then
            echo "$backup_node_disk/$cam_id is not exist, do nothing."
            continue
        fi

        find $backup_node_disk/$cam_id -name 'hr_[0-2][0-9].c[0-9]' -type f -mtime "+$1" -exec rm -f {} \;
        find $backup_node_disk/$cam_id -name 'day_[0-9][0-9]' -type d -empty -exec rm -r {} \;
        find $backup_node_disk/ -name '$cam_id' -type d -empty -exec rm -r {} \;
    done       
}

if [ $# -eq 0 ];
then
	echo "usage: ./clean_expired_file.sh /var/csv-data/metadata/cam_info"
	exit
fi

if [[ ! -e $1 ]]; then
    echo "file $1 not exist, please check."
    exit 0
fi

# check this is backup node or not
backup_node_flag=`ps -ef | grep "$backup_node_recorder_mark"`
if [[ -z $backup_node_flag ]]; then
    echo "this is not backup node, no need to clean expired file. stop."
    exit 0;
else
    echo "this is backup node, start clean expired file."
fi

while read -r line
do
    if [[ -r $1 ]]; then     # $1 exist and can been read
        if [[ -z $line ]]; then        #line is empty
            echo "line is empty" 
            continue
        else
            columns_of_cam_info=`echo $line | awk -F "," '{print NF}'`
            if [[ $columns_of_cam_info -ne 4 ]]; then
                echo "columns of cam_info is not 4, it may be error, please check."
                continue;
            fi
            
	        cam_id=`echo $line | awk -F "," '{print $3}' | awk -F "=" '{print $NF}'`
	        cam_id_hex=`printf "%08x" $cam_id`
	        expired_time=`echo $line | awk -F "," '{print $4}' | awk -F "=" '{print $NF}'`
            
            if [[ $PERIOD_MIN -gt $expired_time ]] || [[ $PERIOD_MAX -lt $expired_time ]]; then
                echo "expired_time:$expired_time is invaild, it must greater than $PERIOD_MAX and less than $PERIOD_MIN \n"
                break
            else
                echo "cam_id:$cam_id, storage_period:$expired_time"
            fi

            clean_garbage $expired_time $cam_id_hex
        fi
    else
        echo "$1 not exist, please check."
    fi
done < $1
